import { _decorator, Component, director, Event, find, instantiate, Label, labelAssembler, Layout, Node, Prefab, ScrollView, UITransform, v2, Vec2, Vec3 } from 'cc';
import { AudioManager, Audios } from '../AshFramework/Managers/AudioManager';
import { EventManager, MyEvent } from '../AshFramework/Managers/EventManager';
import { HeroItem } from './HeroItem';
import Banner from '../Banner';
import { QUALITY } from '../AshFramework/Datas/Constant';
import { MenuButton } from './MenuButton';
import { GameManager } from '../GameManager';
import { PoolManager } from '../AshFramework/Managers/PoolManager';
import { Panel, UIManager } from '../AshFramework/Managers/UIManager';
import { DataManager } from '../AshFramework/Managers/DataManager';

const { ccclass, property } = _decorator;

@ccclass('HeroPanel')
export class HeroPanel extends Component {

    scrollView: ScrollView = null;
    layout: Layout = null;

    goodContent: Node = null;
    rareContent: Node = null;
    epicContent: Node = null;

    menuButtonBar: Node = null;

    moneyLabel: Label = null;

    items: HeroItem[] = [];

    menuButtons: MenuButton[] = [];

    selectItem: HeroItem = null;

    quality: QUALITY = QUALITY.GOOD;

    protected onLoad(): void {
        this.scrollView = find("ScrollView", this.node).getComponent(ScrollView);
        this.goodContent = find("ScrollView/view/GoodContent", this.node);
        this.rareContent = find("ScrollView/view/RareContent", this.node);
        this.epicContent = find("ScrollView/view/EpicContent", this.node);
        this.menuButtonBar = find("ScrollView/MenuButtonBar", this.node);
        this.moneyLabel = find("Money/Label", this.node).getComponent(Label);

        this.menuButtons.push(find("ScrollView/MenuButtons/GoodButton", this.node).getComponent(MenuButton));
        this.menuButtons.push(find("ScrollView/MenuButtons/RareButton", this.node).getComponent(MenuButton));
        this.menuButtons.push(find("ScrollView/MenuButtons/EpicButton", this.node).getComponent(MenuButton));

        EventManager.on(MyEvent.REFRESH_MONEY, this.RefreshMoney, this);
    }

    start() {
        this.RefreshMoney();

        this.items.forEach(e => PoolManager.PutNode(e.node));

        for (let i = 0; i < DataManager.heroDatas.length; i++) {
            const data = DataManager.heroDatas[i];

            let parent;
            if (data.Quality == 0) parent = this.goodContent;
            if (data.Quality == 1) parent = this.rareContent;
            if (data.Quality == 2) parent = this.epicContent;

            PoolManager.GetNode("Prefabs/UI/HeroItem", parent).then((node: Node) => {
                let item = node.getComponent(HeroItem);
                if (!parent.active) item.onLoad();
                item.Init(data);
                this.items.push(item);
            });
        }

        this.quality = 0;
        this.menuButtonBar.children.forEach(e => e.active = e.name == `${this.quality}`);
        this.Refresh("GoodButton");
        this.menuButtons.forEach(e => e.Refresh("GoodButton"));
    }

    Show(active: boolean) {
        if (active) {
            if (Banner.NeedShowMoreAd) {
                Banner.Instance.ShowBannerAd();
            }
            this.RefreshItems();
        }

        if (this.node.active != active) {
            this.node.active = active;

            this.Refresh("GoodButton");
            this.menuButtons.forEach(e => e.Refresh("GoodButton"))
            this.menuButtonBar.children.forEach(e => e.active = e.name == `${0}`);
        }
    }

    RefreshItems() {
        this.items.forEach(e => e.Refresh());
    }

    Refresh(name: string) {
        this.goodContent.active = name == "GoodButton";
        this.rareContent.active = name == "RareButton";
        this.epicContent.active = name == "EpicButton";

        switch (name) {
            case "GoodButton":
                this.scrollView.content = this.goodContent;
                break;
            case "RareButton":
                this.scrollView.content = this.rareContent;
                break;
            case "EpicButton":
                this.scrollView.content = this.epicContent;
                break;
        }

        this.scrollView.scrollToTop(0.1);
    }

    RefreshMoney() {
        this.moneyLabel.string = `${GameManager.Instance.Money}`;
    }

    OnCloseButtonClick() {
        AudioManager.PlaySound(Audios.ButtonClick);
        this.node.active = false;
    }

    OnVideoGetButtonButtonClick() {
        AudioManager.PlaySound(Audios.ButtonClick);
        Banner.Instance.ShowVideoAd(() => {
            UIManager.GetInstance().ShowPanel(Panel.UnpackPanel);
        });
    }

    OnButtonClick(event: Event) {
        AudioManager.PlaySound(Audios.ButtonClick);

        let quality;
        switch (event.target.name) {
            case "GoodButton":
                quality = QUALITY.GOOD;
                break;
            case "RareButton":
                quality = QUALITY.RARE;
                break;
            case "EpicButton":
                quality = QUALITY.EPIC;
                break;
        }

        this.quality = quality;
        this.menuButtonBar.children.forEach(e => e.active = e.name == `${quality}`);
        this.Refresh(event.target.name);
        this.menuButtons.forEach(e => e.Refresh(event.target.name));
    }

    protected onEnable(): void {
        EventManager.on(MyEvent.REFRESH_SHOPITEMS, this.RefreshItems, this);
    }

    protected onDisable(): void {
        EventManager.off(MyEvent.REFRESH_SHOPITEMS, this.RefreshItems, this);
    }

}